<template>
  <div :class="imageClass" :style="styleColor">
    <img
      :class="transStyle.startRotate ? 'startImg' : ''"
      :style="imgStyle"
      :src="imgStyle.imageAdress"
      alt=""
    />
  </div>
</template>
<script>
export default {
  name: "WidgetImagePlus",
  components: {},
  props: {
    value: Object,
    ispreview: Boolean
  },
  data() {
    return {
      options: {},
      optionsData: {},
      optionsSetup: {},
       //动画数据
      animateName:'none',
      animateDuration: 1,
      animateRepeat: 1,
      animateDelay:1
    };
  },
  computed: {
    transStyle() {
      return this.objToOne(this.options);
    },
    styleColor() {
      return {
        position: this.ispreview ? "absolute" : "static",
        background: this.transStyle.background,
        "text-align": this.transStyle.textAlign,
        width: this.transStyle.width + "px",
        height: this.transStyle.height + "px",
        left: this.transStyle.left + "px",
        top: this.transStyle.top + "px",
        right: this.transStyle.right + "px",
        "--animate-duration": this.animateDuration + "s",
        "--animate-repeat":this.animateRepeat,
        "--animate-delay":this.animateDelay + "s"
      };
    },
    imgStyle() {
      return {
        imageAdress: this.transStyle.imageAdress,
        "border-radius": this.transStyle.borderRadius + "px",
        opacity: this.transStyle.transparency / 100
      };
    },
    imageClass(){
        return `imagebox animate__animated animate__${this.animateName}  animate__delay-1s animate__repeat-1`
    }
  },
  watch: {
    value: {
      handler(val) {
        this.options = val;
        this.optionsData = val.data;
        this.optionsSetup = val.setup;
        this.editorOptions();
      },
      deep: true
    }
  },
  mounted() {
    this.options = this.value;
    this.optionsData = this.value.data;   
    this.optionsSetup = this.value.setup;
    this.editorOptions();
  },
  methods: {
    editorOptions(){           
      this.setOptionsAnimate();
      this.setFieldListener();
    },
     setFieldListener(){
      if(!this.ispreview){
          return ;
       }
      const _this =this;
      const optionsSetup = this.optionsSetup;
      const fieldName = optionsSetup.fieldName;  
      if (!this.$bus._events[fieldName]) {       
          this.$bus.on(fieldName,(data) => {          
            _this.value.setup.imageAdress = data;
          });
      }
    },
    setOptionsAnimate(){
      const optionsSetup = this.optionsSetup;
      this.animateName =  optionsSetup.animateName;       
      this.animateDuration =  optionsSetup.animateDuration;
      this.animateRepeat =  optionsSetup.animateRepeat;
      this.animateDelay =  optionsSetup.animateDelay;
    },
  }
};
</script>

<style scoped lang="scss">
.imagebox {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.imagebox img {
  width: 100%;
  height: 100%;
}
.startImg {
  animation: turn 1s linear infinite;
}
@keyframes turn {
  0% {
    -webkit-transform: rotate(0deg);
  }
  25% {
    -webkit-transform: rotate(90deg);
  }
  50% {
    -webkit-transform: rotate(180deg);
  }
  75% {
    -webkit-transform: rotate(270deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
</style>
